package so.sao.code.service.domain.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import so.sao.code.service.domain.entity.EnterpriseDomain;
import so.sao.code.service.domain.entity.QrcodeDomain;
import so.sao.code.service.domain.event.EnterpriseUrlEvent;
import so.sao.code.service.repository.EnterpriseOperationLogDao;
import so.sao.code.service.repository.EnterpriseDomainDao;
import so.sao.code.service.repository.QrcodeDomainDao;

/**
 * Created by Administrator on 2017/10/20.
 */
@Component
public class EnterpriseUrlHandler {
    @Autowired
    EnterpriseDomainDao enterpriseDomainDao;
    @Autowired
    QrcodeDomainDao qrcodeDomainDao;
    @Autowired
    EnterpriseOperationLogDao actionLog;

    /***
     * 兼容表设计
     */
    @EventListener(condition = "#event.isAddUrl()")
    public void addUrl(EnterpriseUrlEvent event) {
        QrcodeDomain qrcodeDomain = qrcodeDomainDao.findByDomainUrl(event.getUrl());
        Long domainId = 0L;
        if (qrcodeDomain == null) {
            domainId = qrcodeDomainDao.save(new QrcodeDomain(event.getUrl(), 0)).getId();
        } else {
            domainId = qrcodeDomain.getId();
        }
        enterpriseDomainDao.save(new EnterpriseDomain(event.getEseId(), domainId));
    }

    @EventListener(condition = "#event.isDelUrl()")
    public void delUrl(EnterpriseUrlEvent event) {
        QrcodeDomain qrcodeDomain = qrcodeDomainDao.findByDomainUrl(event.getUrl());
        enterpriseDomainDao.deleteByDomainIdAndEseId(qrcodeDomain.getId(), event.getEseId());
    }

    @Async
    @EventListener
    public void log(EnterpriseUrlEvent event) {

    }

}
